package qldc.pm.qlktcsdldc.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import qldc.pm.qlktcsdldc.model.DanhMucSoBanNganh;
import qldc.pm.qlktcsdldc.model.NguoiDung;
import qldc.pm.qlktcsdldc.portlet.QLKTCSDLDCContants;
import qldc.pm.qlktcsdldc.report.SoBanNganhReport;
import qldc.pm.qlktcsdldc.report.TaiKhoanReport;
import qldc.pm.qlktcsdldc.service.DanhMucSoBanNganhLocalServiceUtil;
import qldc.pm.qlktcsdldc.service.DanhMucSoBanNganh_TruongCongDanLocalServiceUtil;
import qldc.pm.qlktcsdldc.service.NguoiDungLocalServiceUtil;
import qldc.pm.qlktcsdldc.utils.FLog;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.servlet.ServletResponseUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.JavaConstants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.User;
import com.liferay.portal.util.PortalUtil;
import com.report.logic.Report;

public class DanhMucSoBanNganhAction {	
	private static final Log _Log = LogFactoryUtil.getLog(DanhMucSoBanNganhAction.class.getName());
	
	public void addSoBanNganh(ActionRequest actionRequest, ActionResponse actionResponse) {

		FLog.Log("addSoBanNganh");
		SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
		String redirect = ParamUtil.getString(actionRequest, "redirect");
		try {
			User user = PortalUtil.getUser(actionRequest);
			if (Validator.isNotNull(user)) {
				String tenSoBanNganh = ParamUtil.getString(actionRequest, "tenSoBanNganh");
				String diaChi = ParamUtil.getString(actionRequest, "diaChi");
				String sDT = ParamUtil.getString(actionRequest, "sDT");
				String fax = ParamUtil.getString(actionRequest, "fax");
				String email = ParamUtil.getString(actionRequest, "email");
				String website = ParamUtil.getString(actionRequest, "website");
				String nguoiLienHe = ParamUtil.getString(actionRequest, "nguoiLienHe");
				
				DanhMucSoBanNganh danhMucSoBanNganh =  DanhMucSoBanNganhLocalServiceUtil.addDanhMucSoBanNganh(tenSoBanNganh, diaChi, sDT, fax, email, website, nguoiLienHe, user);
				if(Validator.isNotNull(danhMucSoBanNganh)){
					FLog.Log("addSoBanNganh  them-moi-so-ban-nganh-thanh-cong");
					SessionErrors.add(actionRequest, "them-moi-so-ban-nganh-thanh-cong");
				}else{
					FLog.Log("addSoBanNganh  them-moi-so-ban-nganh-khong-thanh-cong");
					SessionErrors.add(actionRequest, "them-moi-so-ban-nganh-khong-thanh-cong");
				}
				
			}else {
				SessionErrors.add(actionRequest, "vui-long-dang-nhap");
			}

			if (redirect != null && redirect.length() > 0)
				actionResponse.sendRedirect(redirect);

		}
		catch (PortalException e) {
			_Log.error(e);
		}
		catch (SystemException e) {
			_Log.error(e);
		}
		catch (IOException e) {
			_Log.error(e);
		}
	}
	
	public void updateSoBanNganh(ActionRequest actionRequest, ActionResponse actionResponse) {

		FLog.Log("updateSoBanNganh");
		SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
		String redirect = ParamUtil.getString(actionRequest, "redirect");
		try {
			User user = PortalUtil.getUser(actionRequest);
			if (Validator.isNotNull(user)) {
				long soBanNganhId = ParamUtil.getLong(actionRequest, "soBanNganhId");
				String tenSoBanNghanh = ParamUtil.getString(actionRequest, "tenSoBanNghanh");
				String diaChi = ParamUtil.getString(actionRequest, "diaChi");
				String sDT = ParamUtil.getString(actionRequest, "sDT");
				String fax = ParamUtil.getString(actionRequest, "fax");
				String email = ParamUtil.getString(actionRequest, "email");
				String website = ParamUtil.getString(actionRequest, "website");
				String nguoiLienHe = ParamUtil.getString(actionRequest, "nguoiLienHe");
				
				
				DanhMucSoBanNganh danhMucSoBanNganh =  DanhMucSoBanNganhLocalServiceUtil.updateDanhMucSoBanNganh(soBanNganhId, tenSoBanNghanh, diaChi, sDT, fax, email, website, nguoiLienHe, user);
				if(Validator.isNotNull(danhMucSoBanNganh)){
					FLog.Log("addSoBanNganh  cap-nhat-so-ban-nganh-thanh-cong");
					SessionErrors.add(actionRequest, "cap-nhat-so-ban-nganh-thanh-cong");
				}else{
					FLog.Log("addSoBanNganh  cap-nhat-so-ban-nganh-khong-thanh-cong");
					SessionErrors.add(actionRequest, "cap-nhat-so-ban-nganh-khong-thanh-cong");
				}
				
			}else {
				SessionErrors.add(actionRequest, "vui-long-dang-nhap");
			}

			if (redirect != null && redirect.length() > 0)
				actionResponse.sendRedirect(redirect);

		}
		catch (PortalException e) {
			_Log.error(e);
		}
		catch (SystemException e) {
			_Log.error(e);
		}
		catch (IOException e) {
			_Log.error(e);
		}
	}
	
	public void deleteSoBanNganh(ActionRequest actionRequest, ActionResponse actionResponse) {

		FLog.Log("deleteSoBanNganh");
		SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
		String redirect = ParamUtil.getString(actionRequest, "redirect");
		try {
			User user = PortalUtil.getUser(actionRequest);
			if (Validator.isNotNull(user)) {
				long soBanNganhId = ParamUtil.getLong(actionRequest, "soBanNganhId");
				
				DanhMucSoBanNganh danhMucSoBanNganh =  DanhMucSoBanNganhLocalServiceUtil.deteleDanhMucSoBanNganh(soBanNganhId, user);
				if(Validator.isNotNull(danhMucSoBanNganh)){
					FLog.Log("addSoBanNganh  xoa-so-ban-nganh-thanh-cong");
					SessionErrors.add(actionRequest, "xoa-so-ban-nganh-thanh-cong");
				}else{
					FLog.Log("addSoBanNganh  xoa-so-ban-nganh-khong-thanh-cong");
					SessionErrors.add(actionRequest, "xoa-so-ban-nganh-khong-thanh-cong");
				}
				
			}else {
				SessionErrors.add(actionRequest, "vui-long-dang-nhap");
			}

			if (redirect != null && redirect.length() > 0)
				actionResponse.sendRedirect(redirect);

		}
		catch (PortalException e) {
			_Log.error(e);
		}
		catch (SystemException e) {
			_Log.error(e);
		}
		catch (IOException e) {
			_Log.error(e);
		}
	}
	
	public void savePhanQuyenThaoTac(ActionRequest actionRequest, ActionResponse actionResponse) {
		FLog.Log("savePhanQuyenThaoTac");
		SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
		String redirect = ParamUtil.getString(actionRequest, "redirect");
		try {
			User user = PortalUtil.getUser(actionRequest);
			if (Validator.isNotNull(user)) {
				long soBanNganhId = ParamUtil.getLong(actionRequest, "soBanNganhId");
				FLog.Log("savePhanQuyenThaoTac soBanNganhId:"+ soBanNganhId);
				String[] checkedIds = actionRequest.getParameterValues("rowIds");
				if(checkedIds != null && checkedIds.length > 0){
					DanhMucSoBanNganh_TruongCongDanLocalServiceUtil.updateDanhMucSoBanNganh_TruongCongDan(soBanNganhId, checkedIds, user);
				}
			}else {
				SessionErrors.add(actionRequest, "vui-long-dang-nhap");
			}

			if (redirect != null && redirect.length() > 0)
				actionResponse.sendRedirect(redirect);

		}
		catch (PortalException e) {
			_Log.error(e);
		}
		catch (SystemException e) {
			_Log.error(e);
		}
		catch (IOException e) {
			_Log.error(e);
		}
	}
	
	public void DMSBN_ManHinhChinhAction(ActionRequest actionRequest, ActionResponse actionResponse) {
		String cmd = ParamUtil.getString(actionRequest, "cmd");	
		FLog.Log("DMSBN_ManHinhChinhAction cmd >>>>>>>>>>>>>>"+cmd);
		if (cmd.equals(QLKTCSDLDCContants.IN)){
			DMSBN_ManHinhChinhIn(actionRequest, actionResponse);
		} else if(cmd.equals(QLKTCSDLDCContants.ADD)){
			String redirect = ParamUtil.getString(actionRequest, "addRedirect");
			if (redirect != null && redirect.length() > 0)
				try {
					actionResponse.sendRedirect(redirect);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}

	private void DMSBN_ManHinhChinhIn(ActionRequest actionRequest, ActionResponse actionResponse) {
		// TODO Auto-generated method stub
		FLog.Log("DMSBN_ManHinhChinhIn");
		SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
		String redirect = ParamUtil.getString(actionRequest, "redirect");
		try {
			User user = PortalUtil.getUser(actionRequest);
			if (Validator.isNotNull(user)) {
				String tenSoBanNganh = ParamUtil.getString(actionRequest, "tenSoBanNganh");
				
				List<DanhMucSoBanNganh> results = DanhMucSoBanNganhLocalServiceUtil.searchByTenSoBanNganh(tenSoBanNganh);
				
				List<SoBanNganhReport> soBanNganhReports = new ArrayList<SoBanNganhReport>();
				for(int i=0; i < results.size(); i++){
					DanhMucSoBanNganh soBanNganh = results.get(i);
					SoBanNganhReport soBanNganhReport = new SoBanNganhReport();
					soBanNganhReport.setStt((i+1)+"");
					soBanNganhReport.setTenSoBanNganh(soBanNganh.getTenSoBanNganh());
					soBanNganhReport.setDiaChi(soBanNganh.getDiaChi());
					soBanNganhReport.setSoDienThoai(soBanNganh.getSoDienThoai());
					soBanNganhReport.setFax(soBanNganh.getFax());
					soBanNganhReport.setEmail(soBanNganh.getEmail());
					soBanNganhReport.setWebsite(soBanNganh.getWebsite());
					soBanNganhReport.setNguoiLienHe(soBanNganh.getNguoiLienHe());
					
					soBanNganhReports.add(soBanNganhReport);
				}
			
				// create file PDF
				HashMap<String, Object> data = new HashMap<String, Object>();
				data.put("tenSoBanNganh", tenSoBanNganh);
				data.put("list_sobannganh", soBanNganhReports);
				
				PortletContext pc = actionRequest.getPortletSession().getPortletContext();
				String reportFileName = "So_ban_nganh";
				String reportFilePath = pc.getRealPath(QLKTCSDLDCContants.REPORT_FOLDER + "/" + reportFileName + ".jrxml");

				File file = Report.createReport(data, Report.DOC_TYPE, reportFilePath, reportFileName);
				if(Validator.isNotNull(file)) {
					InputStream in = new FileInputStream(file);

					HttpServletRequest httpServletRequest = PortalUtil.getHttpServletRequest(actionRequest);
					HttpServletResponse httpServletResponse = PortalUtil.getHttpServletResponse(actionResponse);
					ServletResponseUtil.sendFile(httpServletRequest, httpServletResponse, reportFileName + "." + Report.DOC_TYPE, in, Report.DOC_STREAM_TYPE);
				}
				
				SessionMessages.add(actionRequest, "file-in-duoc-mo-vui-long-kiem-tra-file-in");

				FLog.Log("DMSBN_ManHinhChinhIn  cap-nhat-thanh-cong:");
			}
			else {
				SessionErrors.add(actionRequest, "vui-long-dang-nhap");
			}

			if (redirect != null && redirect.length() > 0)
				actionResponse.sendRedirect(redirect);

		}
		catch (PortalException e) {
			_Log.error(e);
		}
		catch (SystemException e) {
			_Log.error(e);
		}
		catch (IOException e) {
			_Log.error(e);
		}
	}
}
